import Vue from 'vue'
import VueRouter from 'vue-router'

// 普通方式
// import Login from "components/Login.vue"
// import Home from 'components/Home.vue'
// import Welcome from "components/Welcome.vue"

// 懒加载: 登录——主页——欢迎页
const Login = () => import(/* webpackChunkName: "login_Home_Welcome" */ "views/Login.vue")
const Home = () => import(/* webpackChunkName: "login_Home_Welcome" */ "views/Home.vue")
const Welcome = () => import(/* webpackChunkName: "login_Home_Welcome" */ "components/welcome")

// 用户管理
const UserView = () => import(/* webpackChunkName: "order_Report" */ "views/UserView.vue")

// 权限管理
const RightsList = () => import(/* webpackChunkName: "rights" */ "components/power/Rights.vue")
const RolesList = () => import(/* webpackChunkName: "rights" */ "components/power/Roles.vue")

// 商品管理
const Categories = () => import(/* webpackChunkName: "goods" */ "components/goods/Categories.vue")
const Params = () => import(/* webpackChunkName: "goods" */ "components/goods/Params.vue")
const List = () => import(/* webpackChunkName: "goods" */ "components/goods/List.vue")
const AddGoods = () => import(/* webpackChunkName: "goods" */ "components/goods/AddGoods.vue")

// 订单管理
const Order = () => import(/* webpackChunkName: "order_Report" */ "components/order/Order.vue")

// 数据可视化
const DataView = () => import(/* webpackChunkName: "order_Report" */ "views/DataView.vue")
const PersonalCenter = () => import(/* webpackChunkName: "order_Report" */ "views/PersonalCenter.vue")
const Building = () => import(/* webpackChunkName: "order_Report" */ "views/Building.vue")
const Room = () => import(/* webpackChunkName: "order_Report" */ "views/Room.vue")

//小区管理
const OwnerView = () => import("@/views/OwnerView.vue")

//互动管理
const InteractionView = () => import("@/views/InteractionView.vue")

//角色管理
const RoleView = () => import("@/views/RoleView.vue")
//部门管理
const DeptView = () => import("@/views/DeptView.vue")
//小区信息
const CommunityView = () => import("@/views/CommunityView.vue")
//投诉建议
const ComplaintView = () => import("@/views/ComplaintView.vue")

//菜单界面
const Menu = () => import("views/SysMenuView.vue")
//岗位界面
const Post = () => import("views/SysPostView.vue")
// 业主审核
const Owner = () => import("views/ZyOwnerView.vue")
// 访客邀请
const Visitor = () => import("views/ZyVisitorView.vue")
const SysDictTypeView = () => import("views/SysDictTypeView.vue")
const JumpDictTypeVue = () => import("views/JumpDictTypeVue.vue")
const LogOperView = () => import("views/LogOperView.vue")
const LogLoginView = () => import("views/LogLoginView.vue")
const UnitView = () => import("views/UnitView.vue")

Vue.use(VueRouter)

const routes = [
    {path: '/', redirect: '/login'},
    {path: '/login', component: Login},
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            {path: '/welcome', component: Welcome},
            {path: '/rights', component: RightsList},
            {path: '/roles', component: RolesList},
            {path: '/categories', component: Categories},
            {path: '/params', component: Params},
            {path: '/goods', component: List},
            {path: '/goods/add', component: AddGoods},
            {path: '/orders', component: Order},
            {path: '/user', component: UserView},
            {path: '/owner', component: OwnerView},
            {path: '/interaction', component: InteractionView},
            {path: '/role', component: RoleView},
            {path: '/dept', component: DeptView},
            {path: '/community', component: CommunityView},
            {path: '/suggest', component: ComplaintView},
            {path: '/roles', component: RolesList},
            {path: '/categories', component: Categories},
            {path: '/params', component: Params},
            {path: '/goods', component: List},
            {path: '/goods/add', component: AddGoods},
            {path: '/orders', component: Order},
            {path: '/user', component: UserView},
            {path: '/menu', component: Menu},
            {path: '/post', component: Post},
            {path: '/ownerRoom', component: Owner},
            {path: '/visitor', component: Visitor},
            {path: '/personal', component: PersonalCenter},
            {path: '/building', component: Building},
            { path: '/dict', component: SysDictTypeView},
            { path: '/JumpDictTypeVue',name:'JumpDictTypeVue', component: JumpDictTypeVue },
            { path: '/operlog', component: LogOperView },
            { path: '/logininfor', component: LogLoginView },
            {path: '/room', component: Room},
            { path: '/unit', component: UnitView },
        ]
    },
    {
        path: '/dataview',
        component: DataView
    }
]


const router = new VueRouter({
    // mode: 'history',
    routes
})

// 挂载导航守卫

// from从哪儿来 to到哪儿去 next放行函数
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()

    const accessTokenStr = localStorage.getItem('access-token')
    const refreshTokenStr = localStorage.getItem('refresh-token')
    const tokenStr = accessTokenStr + refreshTokenStr;
    if (!tokenStr) {
        Vue.prototype.$message({
            type: 'info',
            message: '小伙子,请登录',
        })
        return next('/login')
    }
    next()
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }


export default router
